﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Complex
{
    private double realNumber;
    private double imaginaryNumber;

    public double ImaginaryNumber
    {
        get { return imaginaryNumber; }
    }

    public double RealNumber
    {
        get { return realNumber; }
    }

    public Complex(double realNumber, double imaginaryNumber)
    {
        this.realNumber = realNumber;
        this.imaginaryNumber = imaginaryNumber;
    }

    public static Complex operator +(Complex number1, Complex number2)
    {
        return new Complex(number1.realNumber + number2.realNumber, number1.imaginaryNumber + number2.imaginaryNumber);
    }

    public static Complex operator -(Complex number1, Complex number2)
    {
        return new Complex(number1.realNumber - number2.realNumber, number1.imaginaryNumber - number2.imaginaryNumber);
    }

    public static Complex operator *(Complex number1, Complex number2)
    {
        double realNumber = (number1.realNumber * number2.realNumber) - (number1.imaginaryNumber * number2.imaginaryNumber);
        double imaginaryNumber = number1.realNumber * number2.imaginaryNumber + number2.realNumber * number1.imaginaryNumber;
        return new Complex(realNumber, imaginaryNumber);
    }

    public static Complex operator -(Complex complexNumber)
    {
        return new Complex(-complexNumber.realNumber, -complexNumber.imaginaryNumber);
    }

    public static Complex operator /(Complex number1, Complex number2)
    {
        double realNumber = (number1.realNumber * number2.realNumber + number1.imaginaryNumber * number2.imaginaryNumber) /
            (number2.realNumber * number2.realNumber + number2.imaginaryNumber * number2.imaginaryNumber);

        double imaginaryNumber = (number1.imaginaryNumber * number2.realNumber - number1.realNumber * number2.imaginaryNumber) /
            (number2.realNumber * number2.realNumber + number2.imaginaryNumber * number2.imaginaryNumber);

        return new Complex(realNumber, imaginaryNumber);
    }

    public static implicit operator Complex(double d)
    {
        return new Complex(d, 0);
    }

    public static explicit operator double(Complex c)
    {
        if (c.imaginaryNumber != 0)
        {
            return double.NaN;
        }
        return c.realNumber;
    }
    public override string ToString()
    {
        return (realNumber + " + " + imaginaryNumber + " * i");
    }

}
